package com.xt.partjobback.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xt.partjobback.dto.JobDto;
import com.xt.partjobback.entity.Collect;
import com.xt.partjobback.entity.Job;
import com.xt.partjobback.mapper.ClassifyMapper;
import com.xt.partjobback.mapper.CollectMapper;
import com.xt.partjobback.mapper.JobMapper;
import com.xt.partjobback.service.JobService;
import com.xt.partjobback.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author leotong
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService {

    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public Page<Job> getJobByCondition(Job job) {
        Page<Job> page=new Page<>(job.pageNum==null?1: job.getPageNum(),job.pageSize==null?5: job.getPageSize());
        QueryWrapper<Job> queryWrapper=new QueryWrapper<>();
        if (job.getTitle()!=null&&!job.getTitle().equals("")){
            queryWrapper.like("title",job.getTitle());
        }
        if (job.getContent()!=null&&!job.getContent().equals("")){
            queryWrapper.like("content",job.getContent());
        }
        if (job.getCid()!=null){
            queryWrapper.like("cid",job.getCid());
        }
        return jobMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<JobDto> getJobByUser(Job job) {
        Page<Job> jobPage = this.getJobByCondition(job);
        Page<JobDto> jobDtoPage=new Page<>();
        jobDtoPage.setTotal(jobPage.getTotal());
        jobDtoPage.setRecords(new ArrayList<>());
        int token = TokenUtil.tokenExtractId(job.getToken());
        System.out.println("当前用户:"+token);
        for (Job record : jobPage.getRecords()) {
            JobDto jobDto=new JobDto();
            BeanUtils.copyProperties(record,jobDto);
            jobDto.setUseCollect(collectMapper.selectOne(new QueryWrapper<Collect>().eq("uid",token).eq("jid",record.getId()))!=null);
            jobDtoPage.getRecords().add(jobDto);
        }
        return jobDtoPage;
    }
}
